package com.rt.nettysocketio;

import com.corundumstudio.socketio.SocketConfig;
import com.corundumstudio.socketio.SocketIONamespace;
import com.corundumstudio.socketio.SocketIOServer;
import com.rt.nettysocketio.dto.PushMessage;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * TODO().
 *
 * <p>TODO().
 *
 * <p>版权所有:Copyright (c) 2019<br/>
 *
 * @version V1.0
 * @since 2020/6/4 15:28
 */
@Configuration
public class SocketIOConfig {

        @Value("${socketio.host}")
        private String host;

        @Value("${socketio.port}")
        private Integer port;

        @Value("${socketio.bossCount}")
        private int bossCount;

        @Value("${socketio.workCount}")
        private int workCount;

        @Value("${socketio.allowCustomRequests}")
        private boolean allowCustomRequests;

        @Value("${socketio.upgradeTimeout}")
        private int upgradeTimeout;

        @Value("${socketio.pingTimeout}")
        private int pingTimeout;

        @Value("${socketio.pingInterval}")
        private int pingInterval;

        /**
         * 以下配置在上面的application.properties中已经注明
         * @return
         */
        @Bean
        public SocketIOServer socketIOServer() {

            SocketConfig socketConfig = new SocketConfig();
            socketConfig.setTcpNoDelay(true);
            socketConfig.setSoLinger(0);
            com.corundumstudio.socketio.Configuration config = new com.corundumstudio.socketio.Configuration();
            config.setSocketConfig(socketConfig);
            config.setHostname(host);
            config.setPort(port);
            config.setBossThreads(bossCount);
            config.setWorkerThreads(workCount);
            config.setAllowCustomRequests(allowCustomRequests);
            config.setUpgradeTimeout(upgradeTimeout);
            config.setPingTimeout(pingTimeout);
            config.setPingInterval(pingInterval);
            SocketIOServer socketIOServer = new SocketIOServer(config);
            return  socketIOServer;
        }

}
